package com.nangle.managehost.manager;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.nangle.managehost.db.DBManager;
import com.nangle.managehost.db.SQLiteTemplate;

import com.nangle.managehost.entity.Bed;
import com.nangle.managehost.entity.Room;

import java.util.List;

/**
 * Created by YFB on 2017/4/13.
 */

public class RoomManager {
    private static RoomManager roomManager = null;
    private static DBManager manager = null;
    private RoomManager(Context context) {
        manager = DBManager.getInstance(context,  "yjhcloud");
    }
    public static RoomManager getInstance(Context context) {

        if (roomManager == null) {
            roomManager = new RoomManager(context);
        }

        return roomManager;
    }
    public long saveRoom(Room patient){
        SQLiteTemplate st = SQLiteTemplate.getInstance(manager, false);
        ContentValues contentValues = new ContentValues();

        contentValues.put("roomnumber",patient.getRoomnumber());
        contentValues.put("responsibility_nurse", patient.getNurse());
        contentValues.put("type", patient.getType());
        contentValues.put("nurseLeader", patient.getNurseLeader());
        contentValues.put("doctorLeader", patient.getDoctorLeader());
        contentValues.put("doctor", patient.getDoctor());

        return st.insert("room", contentValues);
    }
    public List<Room> getRoomList(){
       SQLiteTemplate st = SQLiteTemplate.getInstance(manager, false);
        List<Room> list = st.queryForList(new SQLiteTemplate.RowMapper<Room>() {

            @Override
            public Room mapRow(Cursor cursor, int index) {
                Room notice = new Room();
                notice.setId(cursor.getString(cursor.getColumnIndex("_id")));
                notice.setRoomnumber(cursor.getInt(cursor
                        .getColumnIndex("roomnumber")));
                notice.setNurse(cursor.getString(cursor.getColumnIndex("responsibility_nurse")));
                notice.setType(cursor.getInt(cursor
                        .getColumnIndex("type")));
                notice.setNurseLeader(cursor.getString(cursor.getColumnIndex("nurseLeader")));
                notice.setDoctor(cursor.getString(cursor.getColumnIndex("doctor")));
                notice.setDoctorLeader(cursor.getString(cursor.getColumnIndex("doctorLeader")));



                return notice;
            }

        }, "select * from room" , null);
        return list;
    }




    public Room getRoom(String id){
        SQLiteTemplate st = SQLiteTemplate.getInstance(manager, false);
        return st.queryForObject(new SQLiteTemplate.RowMapper<Room>() {

            @Override
            public Room mapRow(Cursor cursor, int index) {
                Room notice = new Room();
                notice.setId(cursor.getString(cursor.getColumnIndex("_id")));
                notice.setRoomnumber(cursor.getInt(cursor
                        .getColumnIndex("roomnumber")));
                notice.setNurse(cursor.getString(cursor.getColumnIndex("responsibility_nurse")));
                notice.setType(cursor.getInt(cursor
                        .getColumnIndex("type")));
                notice.setNurseLeader(cursor.getString(cursor.getColumnIndex("nurseLeader")));
                notice.setDoctor(cursor.getString(cursor.getColumnIndex("doctor")));
                notice.setDoctorLeader(cursor.getString(cursor.getColumnIndex("doctorLeader")));
                return notice;
            }

        }, "select * from room where _id=?", new String[] { id });
    }
}
